package com.five.welcome.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.five.welcome.entity.Dormitoryallocation;
import com.five.welcome.entity.statistic.RoomOfStudentCount;
import com.five.welcome.result.Result;
import com.five.welcome.service.DormitoryallocationService;
import com.five.welcome.service.WelcomeProgressService;
import com.five.welcome.utils.SNumberUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.SmartFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ywt
 * @since 2024-07-12
 */
@RestController
@CrossOrigin
@Tag(name = "宿舍接口")
@RequestMapping("/dormitoryallocation")
public class DormitoryallocationController {

    @Autowired
    private DormitoryallocationService dormitoryallocationService;

    @Autowired
    private WelcomeProgressService welcomeProgressService;

    //查询所有宿舍信息
    @Operation(summary = "查询所有宿舍信息")
    @GetMapping("/getAllDormitory")
    public Result getAllDormitory() {
        List<Dormitoryallocation> dormitoryallocations = dormitoryallocationService.getAll();
        return Result.ok(dormitoryallocations);
    }

    //根据学号分配宿舍
    @Operation(summary = "根据学号分配宿舍")
    @GetMapping("/giveRoomBySNumber")
    public Result giveRoomBySNumber(String currentNumber) {
        Map<String, Object> map = dormitoryallocationService
                .giveRoomBySNumber(currentNumber);
        if (map == null) {
            return Result.error("已有宿舍信息!");
        }
        welcomeProgressService.updateValue("roomed", currentNumber);
        return Result.ok(map);
    }

    //查询是否有宿舍信息
    @Operation(summary = "查询是否有宿舍信息")
    @GetMapping("/isHaveRoom")
    public Result isHaveRoom(String currentNumber) {
        Dormitoryallocation dormitoryBySNumber =
                dormitoryallocationService.getDormitoryBySNumber(currentNumber);
        if (dormitoryBySNumber.getBuildingNumber() == null) {
            //没宿舍 200
            return Result.ok();
        }
        //有宿舍 500
        return Result.error(dormitoryBySNumber);
    }


    //选择宿舍
    @Operation(summary = "选择宿舍")
    @PostMapping("/selectRoom")
    public Result selectRoom(String roomBuilding, String roomNumber, String bedNumber,String sNumber) {
        int end = dormitoryallocationService.selectRoom(roomBuilding, roomNumber, bedNumber,sNumber);
        if (end > 0) {
            return Result.ok();
        }
        return Result.error("错误");
    }


    //分页查询所有宿舍信息
    @Operation(summary = "分页查询所有宿舍信息")
    @GetMapping("/getAllDormitoryPage")
    public Result getAllDormitoryPage(@RequestParam(defaultValue = "1") Integer pageNumber,
                                      @RequestParam(defaultValue = "9") Integer pageSize,
                                      String sNumber, String sname,
                                      Integer buildingNumber, Integer dormitoryNumber) {

        Page<Dormitoryallocation> page = dormitoryallocationService
                .getAllDormitoryPage(pageNumber, pageSize, sNumber, sname,
                        buildingNumber, dormitoryNumber);
        return Result.ok(page);
    }

    //统计各个宿舍楼的学生人数
    @Operation(summary = "统计各个宿舍楼的学生人数")
    @GetMapping("/RoomOfStudentCount")
    public Result RoomOfStudentCount() {
        List<RoomOfStudentCount> end = dormitoryallocationService.RoomOfStudentCount();
        return end != null ? Result.ok(end) : Result.error("数据丢失");
    }
}

